#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
#include"decomposition.h"
#include"jobs.h"


char pwd[100];
char user[100];
char path[100];
char home[100];

void interne_cd()
{
  int i;
  if (strcmp(mots[0][1], "../") == 0)
  {
    i = strlen(pwd);
    while (pwd[i] != '/' && i>0)
    {
      i--;
    }
    pwd[i]='\0';
  }
  else if (strcmp(mots[0][1], "~") == 0)
  {
    strcpy(pwd,home);
  }
  else
  {
    DIR * rep = opendir(pwd);
    if (rep != NULL)
    {
      struct dirent * ent;
      int found = 0;
      while ((ent = readdir(rep)) != NULL)
      {
        if (strcmp(ent->d_name,mots[0][1]) == 0)
        {
          //printf("Shell trouve ! \n");
          found = 1;
          int l = strlen(pwd);
          int l2 = strlen(mots[0][1]);
          pwd[l]='/';
          for (i=1; i<=l2; i++)
          {
            pwd[l+i]=mots[0][1][i-1];
          }
          pwd[l+l2+1]='\0';
        }
      }
      if (found == 0)
      {
         printf("bash: cd: %s: Aucun fichier ou dossier de ce type\n",mots[0][1]);
      }
      closedir(rep);
    }
  }
}

void interne_jobs()
{
	afficher_taches();
}